import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index'),
        hidden: false
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    hidden: false,
    meta: {
      title: 'dashboard',
      icon: 'user',
      route: '/',
      roles: ['*']
    },
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'dashboard',
        hidden: false,
        meta: { title: 'dashboard', icon: 'dashboard', affix: true }
      },
      {
        path: '/404',
        component: () => import('@/views/error-page/404'),
        hidden: true
      },
      {
        path: '/401',
        component: () => import('@/views/error-page/401'),
        hidden: true
      },
      {
        path: '/403',
        component: () => import('@/views/error-page/403'),
        hidden: true
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/admin',
    component: Layout,
    redirect: '/admin/user/list',
    alwaysShow: true,
    name: 'AdminUser',
    hidden: false,
    meta: {
      title: '系统管理',
      icon: 'gear',
      route: 'admin',
      roles: ['*']
    },
    children: [
      {
        path: 'user/list',
        component: () => import('@/views/admin/admin-user-list'),
        name: 'AdminUserList',
        hidden: false,
        meta: {
          title: '管理员',
          route: 'admin/user/list',
          roles: ['admin']
        }
      },
      {
        path: 'role/list',
        component: () => import('@/views/admin/admin-role-list'),
        name: 'AdminRoleList',
        hidden: false,
        meta: {
          title: '系统角色',
          route: 'admin/role/list',
          roles: ['admin']
        }
      },
      {
        path: 'permission/list',
        component: () => import('@/views/admin/admin-permission-list'),
        name: 'AdminPermissionList',
        hidden: false,
        meta: {
          title: '系统权限',
          route: 'admin/permission/list',
          roles: ['admin']
        }
      },
      {
        path: 'menu/list',
        component: () => import('@/views/admin/admin-menu-list'),
        name: 'AdminMenuList',
        hidden: false,
        meta: {
          title: '系统菜单',
          route: 'admin/menu/list',
          roles: ['admin']
        }
      },
      {
        path: 'configures/list',
        component: () => import('@/views/system/system-configures-list'),
        name: 'SystemConfiguresList',
        hidden: false,
        meta: {
          title: '系统设置',
          route: 'system/configures/list',
          roles: ['admin']
        }
      },
      {
        path: 'modules/list',
        component: () => import('@/views/system/system-module-list'),
        name: 'SystemModulesList',
        hidden: false,
        meta: {
          title: '模块列表',
          route: 'system/modules/list',
          roles: ['admin']
        }
      },
      {
        path: 'modules/add/:id',
        component: () => import('@/views/system/system-module-add'),
        name: 'SystemModulesAdd',
        hidden: true,
        meta: {
          title: '添加模块',
          route: 'system/modules/add',
          roles: ['admin']
        }
      }
    ]
  },
  {
    path: '/content',
    component: Layout,
    redirect: '/content/node/list',
    alwaysShow: true,
    name: 'Content',
    hidden: false,
    meta: {
      title: '内容管理',
      icon: 'education',
      route: 'content',
      roles: ['*']
    },
    children: [
      {
        path: 'node/list',
        component: () => import('@/views/content/node/node-list'),
        name: 'NodeList',
        hidden: false,
        meta: {
          title: '内容管理',
          route: 'content/node/list',
          roles: ['*']
        }
      },
      {
        path: 'node/add/:id',
        component: () => import('@/views/content/node/node-add'),
        name: 'NodeAdd',
        hidden: true,
        meta: {
          title: '添加内容',
          route: 'content/node/add',
          roles: ['*']
        }
      },
      {
        path: 'node-comment/list',
        component: () => import('@/views/content/node_comment/node-comment-list'),
        name: 'NodeCommentList',
        hidden: false,
        meta: {
          title: '评论管理',
          route: 'content/node_comment/list',
          roles: ['*']
        }
      },
      {
        path: 'node-category/list',
        component: () => import('@/views/content/node_category/node-category-list'),
        name: 'NodeCategoryList',
        hidden: false,
        meta: {
          title: '分类管理',
          route: 'content/node_category/list',
          roles: ['*']
        }
      },
      {
        path: 'node-category/add/:id',
        component: () => import('@/views/content/node_category/node-category-add'),
        name: 'NodeCategoryAdd',
        hidden: true,
        meta: {
          title: '分类添加',
          route: 'content/node_category/add',
          roles: ['*']
        }
      },
      {
        path: 'tags/list',
        component: () => import('@/views/content/tags/tags-list'),
        name: 'TagsList',
        hidden: false,
        meta: {
          title: '标签管理',
          route: 'content/tags/list',
          roles: ['*']
        }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/list',
    alwaysShow: true,
    name: 'User',
    hidden: false,
    meta: {
      title: '用户管理',
      icon: 'user',
      route: 'user',
      roles: ['*']
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/user/users/user-list'),
        name: 'UserList',
        hidden: false,
        meta: {
          title: '用户列表',
          route: 'user/list',
          roles: ['*']
        }
      },
      {
        path: 'account_flow/list',
        component: () => import('@/views/user/account_flow_reports/account_flow_reports-list'),
        name: 'UserAccountFlowList',
        hidden: false,
        meta: {
          title: '流转记录',
          route: 'account_flow/list',
          roles: ['*']
        }
      },
      {
        path: 'role/list',
        component: () => import('@/views/user/user_roles/user-role-list'),
        name: 'UserRoleList',
        hidden: false,
        meta: {
          title: '用户角色',
          route: 'user/role/list',
          roles: ['*']
        }
      }
    ]
  },
  {
    path: '/commerce',
    component: Layout,
    redirect: '/commerce/merchant/list',
    alwaysShow: true,
    name: 'Merchant',
    hidden: false,
    meta: {
      title: '商城管理',
      icon: 'shopping',
      route: 'commerce',
      roles: ['*']
    },
    children: [
      {
        path: 'merchant/list',
        component: () => import('@/views/commerce/merchant/merchant-list'),
        name: 'MerchantList',
        hidden: false,
        meta: {
          title: '商城列表',
          route: 'commerce/merchant/list',
          roles: ['*']
        }
      },
      {
        path: 'order/list',
        component: () => import('@/views/commerce/orders/orders-list'),
        name: 'OrderList',
        hidden: false,
        meta: {
          title: '订单列表',
          route: 'commerce/order/list',
          roles: ['*']
        }
      },
      {
        path: 'goods/list',
        component: () => import('@/views/commerce/goods/goods-list'),
        name: 'GoodsList',
        hidden: false,
        meta: {
          title: '商品列表',
          route: 'commerce/goods/list',
          roles: ['*']
        }
      },
      {
        path: 'goods_set/list',
        component: () => import('@/views/commerce/goods_set/goods-set-list'),
        name: 'GoodsList',
        hidden: false,
        meta: {
          title: '商品集合',
          route: 'commerce/goods_set/list',
          roles: ['*']
        }
      },
      {
        path: 'brand/list',
        component: () => import('@/views/user/users/user-list'),
        name: 'BrandList',
        hidden: false,
        meta: {
          title: '品牌列表',
          route: 'commerce/brand/list',
          roles: ['*']
        }
      },
      {
        path: 'goods_category/list',
        component: () => import('@/views/user/users/user-list'),
        name: 'GoodsCategoryList',
        hidden: false,
        meta: {
          title: '品牌列表',
          route: 'commerce/goods_category/list',
          roles: ['*']
        }
      }
    ]
  },
  {
    path: '/sh',
    component: Layout,
    redirect: '/sh/goods/list',
    alwaysShow: true,
    name: 'SecondHand',
    hidden: false,
    meta: {
      title: '二手管理',
      icon: 'example',
      route: 'sh',
      roles: ['*']
    },
    children: [
      {
        path: 'goods/list',
        component: () => import('@/views/secondhand/goods/goods-list'),
        name: 'GoodsList',
        hidden: false,
        meta: {
          title: '商品列表',
          route: 'sh/goods/list',
          roles: ['*']
        }
      },
      {
        path: 'comment/list',
        component: () => import('@/views/secondhand/goods_comments/goods-comments-list'),
        name: 'GoodsCommentsList',
        hidden: false,
        meta: {
          title: '商品留言',
          route: 'sh/comment/list',
          roles: ['*']
        }
      }
    ]
  },
  {
    path: '/urban',
    component: Layout,
    redirect: '/urban/tour/list',
    alwaysShow: true,
    name: 'Urban',
    hidden: false,
    meta: {
      title: '旅游管理',
      icon: 'example',
      route: 'urban',
      roles: ['*']
    },
    children: [
      {
        path: 'tour/list',
        component: () => import('@/views/urban/tour_elements/tour-elements-list'),
        name: 'TourList',
        hidden: false,
        meta: {
          title: '景点列表',
          route: 'urban/tour/list',
          roles: ['*']
        }
      },
      {
        path: 'restaurant/list',
        component: () => import('@/views/urban/restaurant_elements/restaurant-elements-list'),
        name: 'RestaurantList',
        hidden: false,
        meta: {
          title: '美食列表',
          route: 'urban/restaurant/list',
          roles: ['*']
        }
      },
      {
        path: 'fun/list',
        component: () => import('@/views/urban/fun_elements/fun-elements-list'),
        name: 'SightsList',
        hidden: false,
        meta: {
          title: '玩乐列表',
          route: 'urban/fun/list',
          roles: ['*']
        }
      },
      {
        path: 'convenient/list',
        component: () => import('@/views/urban/convenient_elements/convenient-elements-list'),
        name: 'ConvenientList',
        hidden: false,
        meta: {
          title: '便民列表',
          route: 'urban/convenient/list',
          roles: ['*']
        }
      }
    ]
  },
  {
    path: '/property',
    component: Layout,
    redirect: '/property/list',
    alwaysShow: true,
    name: 'Property',
    hidden: false,
    meta: {
      title: '租售管理',
      icon: 'example',
      route: 'property',
      roles: ['*']
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/property/property_elements/property-elements-list'),
        name: 'PropertyList',
        hidden: false,
        meta: {
          title: '租售列表',
          route: 'property/list',
          roles: ['*']
        }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
